

import Foundation

//API
let login_api  = "http://amicool.neusoft.edu.cn/api.php/login"
let list_api   = "http://amicool.neusoft.edu.cn/api.php/lists"
let logout_api = "http://amicool.neusoft.edu.cn/api.php/logout"
let regist_api = "http://amicool.neusoft.edu.cn/api.php/reg"


typealias NetworkLoginBlock = (_ flag:Int)->Void
typealias NetworkRegistBlock = (_ flag:Int)->Void
typealias NetworListkBlock = (_ List:NSArray?)->Void

class networktool:NSObject{
    
    static var shared = networktool() //单例
    
    static var loginReturnModel:loginjsoninfo? = loginjsoninfo()
    
    func account_login(_ parameters:NSDictionary,block:NetworkLoginBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in parameters{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: login_api)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回数据进行处理
            if data != nil{
                if let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
                    //将返回的数据封装成模型对象
                    networktool.loginReturnModel = loginjsoninfo.returnloginjson(json as? NSDictionary)
                    if networktool.loginReturnModel?.error == nil{
                        block!(1)
                    }
                    else{
                        block!(0)
                    }
                }
            }
        })
        dataTask.resume() //启动任务
    }
    
    func account_regist(_ parameters:NSDictionary,block:NetworkRegistBlock?)
    {
        let paraArray = NSMutableArray()
        for (key,value) in parameters
        {
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: regist_api)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回数据进行处理
            if data != nil{
                if(data?.count == 1)
                {
                    block!(1)
                }
                else
                {
                    block!(0)
                }
            }
        })
        dataTask.resume() //启动任务
    }
    
    func account_logout(_ parameters:NSDictionary)
    {
        let paraArray = NSMutableArray()
        for (key,value) in parameters{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: logout_api)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
    }
    
    func get_list(_ parameters:NSDictionary,block:@escaping NetworListkBlock){
        let paraArray = NSMutableArray()
        for (key,value) in parameters{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: list_api)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        let dataTask = URLSession.shared.dataTask(with: request) {(data, response, error) in
            if data != nil{
                let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
                if json != nil{
                    block(json as? NSArray)
                    //返回给调用者
                }
                else{
                    block(nil)
                }
            }
            else{
                block(nil)
            }
        }
        dataTask.resume() // 使用resume方法启动任务
    }
    
}
